package com.ls.socketio.service.impl;

import com.corundumstudio.socketio.SocketIOServer;
import com.ls.socketio.bean.*;
import com.ls.socketio.config.Properties;
import com.ls.socketio.repository.dao.*;
import com.ls.socketio.service.WebService;
import com.ls.socketio.view.InfoView;
import com.ls.socketio.view.RoomInfoView;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author liShan
 * @date 2019/9/3 11:33
 */
@Service
public class WebServiceImpl implements WebService {

    @Resource
    public SocketIOServer socketIoServer;

    private ListClient listClient = ListClient.getInstance();

    @Resource
    private HotelRepositoryImpl hotelRepositoryImpl;

    @Resource
    private BuildRepositoryImpl buildRepositoryImpl;

    @Resource
    private LayerRepositoryImpl layerRepositoryImpl;

    @Resource
    private RoomRepositoryImpl roomRepositoryImpl;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private Properties properties;

    @Resource
    private RoomBoxRepositoryImpl roomBoxRepositoryImpl;

    @Resource
    private RoomEnvRepositoryImpl roomEnvRepositoryImpl;

    @Resource
    private RoomAirConditionerRepositoryImpl roomAirConditionerRepositoryImpl;

    @Resource
    private RoomBaseRepositoryImpl roomBaseRepositoryImpl;

    @Resource
    private RoomLampRepositoryImpl roomLampRepositoryImpl;

    @Override
    public List<HotelInfo> findHotels() {
        // 查询酒店的信息
        List<HotelInfo> hotelInfos = hotelRepositoryImpl.selectAll();
        hotelInfos.forEach(hotelInfo -> {
            hotelInfo.getBuildInfos().sort(Comparator.comparing(BuildInfo :: getBuildNum));
            hotelInfo.getBuildInfos().forEach(buildInfo -> {
                        buildInfo.getLayerInfos().sort(Comparator.comparing(LayerInfo :: getLayerNum));
                        buildInfo.getLayerInfos().forEach(layerInfo -> layerInfo.setRoomInfos(null));
                    }
            );
        });
        return hotelInfos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addHotel(HotelInfo hotelInfo) {
        hotelInfo.setHotelId(new Random().nextInt());
        hotelRepositoryImpl.addHotel(hotelInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBuild(BuildInfo buildInfo) {
        buildInfo.setBuildId(new Random().nextInt());
        buildRepositoryImpl.addBuild(buildInfo);
    }

    @Override
    public void addLayer(LayerInfo layerInfo) {
        layerInfo.setLayerId(System.currentTimeMillis() + new Random().nextInt());
        layerRepositoryImpl.addLayer(layerInfo);
    }

    @Override
    public void addRoom(RoomInfo roomInfo) {
        roomInfo.setRoomId(System.currentTimeMillis() + new Random().nextInt());
        roomRepositoryImpl.addRoom(roomInfo);
    }

    @Override
    public RoomInfoView sendBox() {
        RoomInfo roomInfo = RoomInfo.builder().roomId(System.currentTimeMillis())
                .roomNum(1)
                .build();
        InfoView view = restTemplate.postForObject(properties.getUrl(), roomInfo, InfoView.class);
        if (null != view){
            return new RoomInfoView(view.getStatusCode(), view.getMessage());
        }
        return new RoomInfoView(5000, "发生未知错误");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void statusAdd(RoomInfo roomInfo) {
        RoomBoxInfo boxInfos = roomInfo.getRoomBoxInfos().get(0);
        // 查询房间id,根据房间的位置
        Long roomId = roomRepositoryImpl.findRoomId(boxInfos);
        // 保存box信息
        boxInfos.setRoomId(roomId);
        roomBoxRepositoryImpl.add(boxInfos);
        if (null != roomInfo.getEnvInfos()){
            RoomEnvInfo envInfo = roomInfo.getEnvInfos().get(0);
            // 保存环境
            envInfo.setEnvId(System.currentTimeMillis());
            envInfo.setRoomId(roomId);
            roomEnvRepositoryImpl.add(envInfo);
        }
        if (null != roomInfo.getAirInfos()){
            RoomAirConditionerInfo airConditionerInfo = roomInfo.getAirInfos().get(0);
            // 保存空调
            airConditionerInfo.setAirId(System.currentTimeMillis() + 1);
            airConditionerInfo.setRoomId(roomId);
            roomAirConditionerRepositoryImpl.add(airConditionerInfo);
        }
        if (null != roomInfo.getBaseInfos()){
            RoomBaseInfo baseInfo = roomInfo.getBaseInfos().get(0);
            // 保存基本信息
            baseInfo.setBaseId(System.currentTimeMillis() + 2);
            baseInfo.setRoomId(roomId);
            roomBaseRepositoryImpl.add(baseInfo);
        }
        if (null != roomInfo.getLampInfos()){
            RoomLampInfo lampInfo = roomInfo.getLampInfos().get(0);
            // 保存灯光
            lampInfo.setLampId(System.currentTimeMillis() + 3);
            lampInfo.setRoomId(roomId);
            roomLampRepositoryImpl.add(lampInfo);
        }
        // 将该房间的信息发送到正在查看的页面进行显示
        for (UUID clientId : listClient.getListClients()) {
            if (socketIoServer.getClient(clientId) != null
                    || listClient.getClientRoom().get(clientId.toString()).contains(roomId)) {
                socketIoServer.getClient(clientId).sendEvent("enewbuy", roomInfo);
            }
        }
    }

}
